package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.UserMapper;
import cn.tedu.egcar1.pojo.dto.UserLoginParam;
import cn.tedu.egcar1.pojo.dto.UserRegParam;
import cn.tedu.egcar1.pojo.entity.User;
import cn.tedu.egcar1.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@Tag(name = "01-用户模块",description = "10")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Operation(summary = "用户注册")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "phone",description = "手机号",required = true),
            @Parameter(name = "email",description = "邮箱",required = true)
    })
    public JsonResult userReg(@RequestBody UserRegParam userRegParam){
        log.debug("userRegParam="+ userRegParam);
        int count = userMapper.selectUsername(userRegParam.getUsername());
        if(count > 0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("开始注册用户对象：" + user);
        int rows = userMapper.insertUser(user);
        if (rows > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult userLogin(@RequestBody UserLoginParam userLoginParam){
        log.debug("userLoginParam="+ userLoginParam);
        UserVO userVO = userMapper.selectUser(userLoginParam);
        if (userVO != null){
            return JsonResult.ok(userVO);
        }
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }













}






